<?php

namespace App\Http\Controllers\Yonghu;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class YuserController extends Controller
{

    public function registr(Request $request){
    	$datas = $request->only(['phone','password']);
		$response = ['phone'=>'手机号','password'=>'密码'];
		if (! empty ( $ex = $this->paramVerifyExist ( 'phone,password') )) return $this->jsonStatus ( "请传递参数[$response[$ex]]" );
		if (! empty ( $ex = $this->paramVerifyEmpty ( 'phone,password' ) )) return $this->jsonStatus ( "[$response[$ex]]不能为空" );
		$datas['password'] = md5($datas['password']);
		$datas['add_time'] = time();
		$datas['up_time'] = time();
		$user_id = DB::table('yusers')->insertGetId($datas);
        $jsonData = isset($user_id) ? $this->getJsonData(1,'注册成功') : $this->getJsonData(0,'注册失败');
		return response()->json($jsonData);
    }

    public function login(Request $request){
    	$datas = $request->only(['phone','password']);
		$response = ['phone'=>'手机号','password'=>'密码'];
		if (! empty ( $ex = $this->paramVerifyExist ( 'phone,password') )) return $this->jsonStatus ( "请传递参数[$response[$ex]]" );
		if (! empty ( $ex = $this->paramVerifyEmpty ( 'phone,password' ) )) return $this->jsonStatus ( "[$response[$ex]]不能为空" );
		$user = DB::table('yusers')->where(['phone'=>$datas['phone'],'password'=>md5($datas['password'])])->first();
		if (!isset($user)) {
			$jsonData = $this->getJsonData(0,'账号密码错误');
		}else{
			//$user_id = $this->sjwtuser();
         	$token = $this->stoken($user->id);
         	$jsonData = $this->getJsonData(1,'登录成功',$token);
		}
		return response()->json($jsonData);
    }

  
}